<?php
function uniq(){
	mt_srand();
	return md5(time().rand(100000,999999));
}
function p($var){
	return var_dump($var);
}
function copy_setting($moduleid){
	$setting_file=EASYSWOOLE_ROOT."/App/Setting/{$moduleid}.php";
	if(!file_exists($setting_file)){
		copy(EASYSWOOLE_ROOT.'/App/Setting/0.php',EASYSWOOLE_ROOT."/App/Setting/{$moduleid}.php");
	}
}
function load_setting($moduleid){
	$setting_file=EASYSWOOLE_ROOT."/App/Setting/{$moduleid}.php";
	$setting=array();
	if(file_exists($setting_file)){
		$setting=include($setting_file);
	}
	return $setting;
}
function save_setting($moduleid,$post){
	$setting_file=EASYSWOOLE_ROOT."/App/Setting/{$moduleid}.php";
	if($post){
		return array_save($post,$setting_file);
	}
	return false;
}
function array_save($array,$file) {
	$data = var_export($array,true);
	$data = "<?php\n return ".$data.';';
	return file_put_contents($file,$data);
}
function pagination($url,$param,$page,$pagesize,$total){
	$total_page=ceil($total/$pagesize);
	$previous_disabled=$next_disabled="";
	if($page<=1){
		$previous_disabled="disabled";
	}
	if($page>=$total_page){
		$next_disabled="disabled";
	}
	$previous_page=max($page-1,1);
	$next_page=min($page+1,$total);

	$p1=http_build_query(array_merge($param,['page'=>$previous_page]));
	$p2=http_build_query(array_merge($param,['page'=>$next_page]));

	if(strpos($url,'?')!==false){
		$url1=$url.'&'.$p1;
		$url2=$url.'&'.$p2;
	}else{
		$url1=$url.'?'.$p1;
		$url2=$url.'?'.$p2;
	}

	$html=<<<st
	<div class="paging_simple_numbers" style="width:400px;margin:0 auto;">
	<ul class="pagination">
	  <li class="paginate_button page-item previous {$previous_disabled}">
	  <a href="{$url1}" class="page-link">上一页</a>
	  </li>
	  <li class="paginate_button page-item active">
	  <a href="javascript:void(0);" class="page-link">{$page}/{$total_page} [共{$total}条]</a>
	  </li>
	  <li class="paginate_button page-item next {$next_disabled}">
	  <a href="{$url2}" class="page-link">下一页</a>
	  </li>
	</ul>
  </div>
st;
	return $html;
}
function get_in_str($arr,$def=null){
	isset($arr) or $arr=array();
	$arr[]=0;
	if($def!==null) $arr[]=$def;
	$arr=array_map('intval',$arr);
	$str=implode(',',$arr);
	return $str;
}
function dsubstr($string, $length, $suffix = '', $start = 0, $charset='utf-8') {
	if($start) {
		$tmp = dsubstr($string, $start);
		$string = substr($string, strlen($tmp));
	}
	$strlen = strlen($string);
	if($strlen <= $length) return $string;
	$string = str_replace(array('&quot;', '&lt;', '&gt;'), array('"', '<', '>'), $string);
	$length = $length - strlen($suffix);
	$str = '';
	if(strtolower($charset) == 'utf-8') {
		$n = $tn = $noc = 0;
		while($n < $strlen)	{
			$t = ord($string{$n});
			if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
				$tn = 1; $n++; $noc++;
			} elseif(194 <= $t && $t <= 223) {
				$tn = 2; $n += 2; $noc += 2;
			} elseif(224 <= $t && $t <= 239) {
				$tn = 3; $n += 3; $noc += 2;
			} elseif(240 <= $t && $t <= 247) {
				$tn = 4; $n += 4; $noc += 2;
			} elseif(248 <= $t && $t <= 251) {
				$tn = 5; $n += 5; $noc += 2;
			} elseif($t == 252 || $t == 253) {
				$tn = 6; $n += 6; $noc += 2;
			} else {
				$n++;
			}
			if($noc >= $length) break;
		}
		if($noc > $length) $n -= $tn;
		$str = substr($string, 0, $n);
	} else {
		for($i = 0; $i < $length; $i++) {
			$str .= ord($string{$i}) > 127 ? $string{$i}.$string{++$i} : $string{$i};
		}
	}
	$str = str_replace(array('"', '<', '>'), array('&quot;', '&lt;', '&gt;'), $str);
	return $str == $string ? $str : $str.$suffix;
}
function array_key_change($arr,$fun='strtolower'){
	$ret=array();
	foreach ($arr as $key => $value) {
		$ret[$fun($key)]=$value;
	}
	return $ret;
}
//二维数组转一维
function multi2single($array){
	$new_array=array();
	$keys=array_keys($array[0]);
	$size=count($keys);
	$n=count($array);
	for($i=0;$i<$n;$i++){
		for($j=0;$j<$size;$j++){
			$key_name=$keys[$j];
			$new_array[$key_name][]=$array[$i][$key_name];
		}
	}
	return $new_array;
}
//二维转一维 单元素
function multi2single_min($array,$key_name){
	$new_array=array();
	if(!is_array($array)) return array();
	if(!is_array($array[0])) return array();
	$n=count($array);
	for($i=0;$i<$n;$i++){
		$new_array[]=$array[$i][$key_name];
	}
	return $new_array;
}
function myswitch($kvalue,$array){
	return  isset($array[$kvalue])?$array[$kvalue]:'';
}
function dhtmlspecialchars($string) {
	if(is_array($string)) {
		return array_map('dhtmlspecialchars', $string);
	} else {
		$string = htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
		return str_replace('&amp;', '&', $string);
	}
}
function dtrim($arr){
	if(is_array($arr)){
		foreach($arr as $k=>$v){
			$arr[$k]=dtrim($v);
		}
	}else{
		$arr=trim($arr);
	}
	return $arr;
}
function kv_builder($arr,$id,$name,$add=array(),$prefix=array()){
    $res=array();
	if(count($prefix)){
		foreach((array) $prefix as $v) $res[$v[0]]=$v[1];
	}
    foreach((array) $arr as $v) $res[$v[$id]]=$v[$name];
	if(count($add)){
		foreach((array) $add as $v) $res[$v[0]]=$v[1];
	}
    return $res;
}
function ka_builder($arr,$id,$name='*',$add=array()){
    $res=array();
	$name_arr=explode(',',$name);
    foreach((array) $arr as $v){
		if($name_arr[0]=='*'){
			$res[$v[$id]]=$v;
		}else{
			$tmp=array();
			foreach($v as $kk=>$vv){
				if(in_array($kk,$name_arr)) $tmp[$kk]=$vv;
			}
			$res[$v[$id]]=$tmp;
		}
	}
	if(count($add)){
		foreach((array) $add as $v) $res[$v[0]]=$v;
	}
    return $res;
}
function kc_builder($arr,$name,$add=array()){
    $res=array();
    foreach((array) $arr as $v) $res[]=$v[$name];
	if(count($add)){
		foreach((array) $add as $v) $res[]=$v;
	}
    return $res;
}
function ks_builder($arr,$id,$name='*',$sep='-',$add=array()){
    $res=array();
	$name_arr=explode(',',$name);
    foreach((array) $arr as $v){
		if($name_arr[0]=='*'){
			$res[$v[$id]]=implode($sep,$v);
		}else{
			$tmp=array();
			foreach($v as $kk=>$vv){
				if(in_array($kk,$name_arr)) $tmp[$kk]=$vv;
			}
			$res[$v[$id]]=implode($sep,$tmp);
		}
	}
	if(count($add)){
		foreach((array) $add as $v) $res[$v[0]]=$v[1];
	}
    return $res;
}
function timeswitch($ts,$format='Y-m-d',$default=''){
    return $ts?date($format,$ts):$default;
}
function post($url,$post_data){
    //初始化
    $curl = curl_init();
    //设置抓取的url
    curl_setopt($curl, CURLOPT_URL, $url);
    //设置头文件的信息作为数据流输出
    curl_setopt($curl, CURLOPT_HEADER, 0);
    //设置获取的信息以文件流的形式返回，而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    //设置post方式提交
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data));
    //执行命令
    $data = curl_exec($curl);
    //关闭URL请求
    curl_close($curl);
    //显示获得的数据
    return $data;
}